<?php
session_start();

// 保存数据库配置到session
if ($_POST) {
    $_SESSION['db_config'] = [
        'host' => $_POST['host'] ?? 'localhost',
        'username' => $_POST['username'] ?? 'root',
        'password' => $_POST['password'] ?? '',
        'database' => $_POST['database'] ?? 'worklog_kukudb_c',
        'charset' => 'utf8mb4'
    ];
    
    // 测试数据库连接
    $config = $_SESSION['db_config'];
    try {
        $dsn = "mysql:host={$config['host']};charset={$config['charset']}";
        $pdo = new PDO($dsn, $config['username'], $config['password']);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        // 检查数据库是否存在
        $stmt = $pdo->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$config['database']}'");
        $databaseExists = $stmt->fetch();
        
        if (!$databaseExists) {
            $_SESSION['db_test'] = ['success' => false, 'message' => '数据库不存在，请先创建数据库: ' . $config['database']];
        } else {
            $_SESSION['db_test'] = ['success' => true, 'message' => '数据库连接成功'];
        }
        header('Location: install.php');
        exit;
    } catch (PDOException $e) {
        $_SESSION['db_test'] = ['success' => false, 'message' => '数据库连接失败: ' . $e->getMessage()];
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>考勤管理系统 - 数据库配置</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .install-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .install-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem;
            text-align: center;
        }
        .install-body {
            padding: 2rem;
        }
        .step-indicator {
            display: flex;
            justify-content: space-between;
            margin-bottom: 2rem;
            position: relative;
        }
        .step-indicator::before {
            content: '';
            position: absolute;
            top: 20px;
            left: 0;
            right: 0;
            height: 2px;
            background: #e9ecef;
            z-index: 1;
        }
        .step {
            text-align: center;
            position: relative;
            z-index: 2;
        }
        .step-number {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #e9ecef;
            color: #6c757d;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 0.5rem;
            font-weight: bold;
        }
        .step.active .step-number {
            background: #667eea;
            color: white;
        }
        .step.completed .step-number {
            background: #28a745;
            color: white;
        }
        .step-label {
            font-size: 0.875rem;
            color: #6c757d;
        }
        .step.active .step-label {
            color: #667eea;
            font-weight: bold;
        }
        .form-group {
            margin-bottom: 1.5rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
                <div class="install-container">
                    <div class="install-header">
                        <h1><i class="fas fa-clock me-2"></i>考勤管理系统</h1>
                        <p class="mb-0">安装向导</p>
                    </div>
                    
                    <div class="install-body">
                        <!-- 步骤指示器 -->
                        <div class="step-indicator">
                            <div class="step completed">
                                <div class="step-number"><i class="fas fa-check"></i></div>
                                <div class="step-label">环境检测</div>
                            </div>
                            <div class="step active">
                                <div class="step-number">2</div>
                                <div class="step-label">数据库配置</div>
                            </div>
                            <div class="step">
                                <div class="step-number">3</div>
                                <div class="step-label">系统安装</div>
                            </div>
                            <div class="step">
                                <div class="step-number">4</div>
                                <div class="step-label">完成安装</div>
                            </div>
                        </div>

                        <h3 class="mb-4">数据库配置</h3>
                        
                        <?php if (isset($_SESSION['db_test']) && !$_SESSION['db_test']['success']): ?>
                            <div class="alert alert-danger">
                                <i class="fas fa-exclamation-triangle me-2"></i>
                                <?php echo $_SESSION['db_test']['message']; ?>
                            </div>
                        <?php endif; ?>
                        
                        <form method="post">
                            <div class="form-group">
                                <label for="host" class="form-label">数据库主机</label>
                                <input type="text" class="form-control" id="host" name="host" 
                                       value="<?php echo $_SESSION['db_config']['host'] ?? 'localhost'; ?>" required>
                                <div class="form-text">通常为 localhost 或 127.0.0.1</div>
                            </div>
                            
                            <div class="form-group">
                                <label for="username" class="form-label">数据库用户名</label>
                                <input type="text" class="form-control" id="username" name="username" 
                                       value="<?php echo $_SESSION['db_config']['username'] ?? 'root'; ?>" required>
                                <div class="form-text">MySQL数据库用户名</div>
                            </div>
                            
                            <div class="form-group">
                                <label for="password" class="form-label">数据库密码</label>
                                <input type="password" class="form-control" id="password" name="password" 
                                       value="<?php echo $_SESSION['db_config']['password'] ?? ''; ?>">
                                <div class="form-text">MySQL数据库密码</div>
                            </div>
                            
                            <div class="form-group">
                                <label for="database" class="form-label">数据库名称</label>
                                <input type="text" class="form-control" id="database" name="database" 
                                       value="<?php echo $_SESSION['db_config']['database'] ?? 'worklog_kukudb_c'; ?>" required>
                                <div class="form-text">请使用您有权限访问的数据库名称</div>
                            </div>
                            
                            <div class="alert alert-info">
                                <i class="fas fa-info-circle me-2"></i>
                                <strong>提示：</strong> 请确保使用您有权限访问的数据库名称。系统默认使用 <code>worklog_kukudb_c</code> 数据库。
                            </div>
                            
                            <div class="d-flex justify-content-between">
                                <a href="index.php" class="btn btn-secondary">
                                    <i class="fas fa-arrow-left me-2"></i>上一步
                                </a>
                                <button type="submit" class="btn btn-primary">
                                    测试连接并继续 <i class="fas fa-arrow-right ms-2"></i>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
